<?php

/*
 * Parse the json array of symbols to buy
 * Wirte them into the symbols file
 * Send an Email with billing information
 */
$File = "gifts.csv";
$Handle = fopen($File, 'a');
$itmes = (array)json_decode(urldecode($_POST['content']['itmes']));
$name = $_POST['content']['name'];
$email = $_POST['content']['email'];
$total = $_POST['content']['total'];
//var_dump($_POST['content']);
//fwrite($Handle, 'Logtime:       '. date('Y-m-d:h-m-s') ."\n");
//$spende = var_dump($itmes, true);

foreach ($itmes as $item) {
    $text = strip_tags($item->text);
    $entry = $item->index . ';' . $item->type . ';' . $item->prize . ';' . $text .';' . date('Y-m-d:h-m-s')  .';' . $name .';' . $email. "\n";
	if($entry != "") {
    		fwrite($Handle, $entry);
	}
    $productname = '';
    switch ($item->type) {
        case 'cat' :
            $productname = "Büsi";
            break;
        case 'sheep' :
            $productname = "Schaf";
            break;
        case 'duck' :
            $productname = "Ente";
            break;
        case 'grill' :
            $productname = "Grill";
            break;
        case 'zelt' :
            $productname = "Hilleberg";
            break;
        case 'feuer' :
            $productname = "Lagerfeuer";
            break;
    }
    $spende = $spende. "\t - ". $productname ." (".$text.")\t:". $item->prize .".- \n";
}

fclose($Handle);

// Send an EMail to Me, Naja and the Sponsor
$subject = "Schafe schenken für Naja und Felix";
$contents = "Lieber/Liebe $name,\nVielen Dank für eure Spende:\n\n";
$contents .= $spende ."\n\n";
$contents .= "Bitte überweise den Gesamtbetrag von CHF $total.- (oder was immer Du beitragen willst) an:\n";
$contents .= "UBS AG, Paradepaltz 6, 8001 Zürich\n";
$contents .= "Felix Nyffenegger\n";
$contents .= "IBAN: CH24 0020 6206 4500 1440 J\n";
$contents .= "BIC: UBSWCHZH80V \n\n";
$contents .= "Liebe Grüsse,\nNaja und Felix";
//echo $contents;
$from_header = 'From: nyfelix@gmail.com';
// mail to supplier
mail($email, $subject, $contents, $from_header);
mail("nyfelix@gmail.com", $subject, $contents, $from_header);
mail("naja.jann@gmail.com", $subject, $contents, $from_header);

// return a vaild JSON String
$result = array("result" => "success");

return json_encode($result)
?>
